/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FFTAnalysis;

import java.io.File;
import java.io.Serializable;

/**
 *
 * @author Joshua
 */
public class NormalizeBean implements Serializable {
private String OutputFile;
private String sampleSizeInBits; 
private String InputFile;
private File in;

  

final int W = 1024; // example window size 
double[] data = new double[0];//W*getOutputFile().getSampleSizeInBits()/8]; // input buffer for reading
double[] inbuf = new double[W]; // input for FFT
double[] fftbuf = new double[W/2]; // output for FFT (half the window size due to +/- frequency aliasing)
 

 /**@brief sets the inputFile path
     * @param newInputPath the new Input Path
     */    
 public String getSampleSizeInBits() {
        return sampleSizeInBits;
    }
    
 
     /**@brief sets the inputFile path
     * @param newInputPath the new Input Path
     */   
 public void setInputFile(String infile) {
        InputFile = infile;
        in = new File(InputFile);
        this.mainFFT();
    }
  
 public static void main(String[] args)
 {
     String test = "F:\\Documents\\200hz.wav";
     //this.setInputFile(test);
 }
 
/* Main read + FFT loop */
public void mainFFT()
{
    // Get the number of audio channels in the wav file
    try
    {
            WavFile wavFile = WavFile.openWavFile(in);
        
                int numChannels = wavFile.getNumChannels();
                //int numChannels = 1; // <-- Assuming an audio file will always be MONO
                
                // Create a buffer of 1024 frames
                double[] buffer = new double[1024 * numChannels];
                
                int framesRead = 0;
                double min = Double.MAX_VALUE;
                double max = Double.MIN_VALUE;
          
                do
                {
                    // Read frames into buffer
                    framesRead = wavFile.readFrames(buffer, 1024);//Grant's machine doesn't work with 1024

                    // Loop through frames and look for minimum and maximum value
                    for (int s=0 ; s<framesRead * numChannels ; s++)
                    {
                        if (buffer[s] > max) {max = buffer[s];}
                        if (buffer[s] < min) {min = buffer[s];}                        
                    }                    
                }
                while (framesRead != 0);
                
                this.decode(buffer, fftbuf);

/*while(true) {
  /* Read an entire buffer's worth */
  /*int numdoublesRead=0;
  while(numdoublesRead < data.length) {
    //int tmp = inFile.readFrames(data,data.length-numdoublesRead);
    if(tmp < 0) { 
      /* EOS */
      /*break;
    }
    numdoublesRead += tmp;
  }
  // Here, data contains the right number of PCM samples
  /* Decode data into inbuf's double[] samples */
  //decode(data,inbuf);
  
//}
    }
    catch (Exception e)
            {
                System.err.println(e);
            }
    
}

/*public static void decode(double[] input, double[] output)
  assert input.length == 2*output.length;   
  
  for(int i=0; i < output.length; i++) 
  {   
    output[i] = (short)(((0xFF & input[2*i+1]) << 8) | (0xFF & input[2*i]));  
    output[i] /= Short.MAX_VALUE; // normalise to [-1,1]   
  }   
}*/




 public void decode(double[] buffer, double[] output, int[] input)
{   
  assert input.length == 2*output.length;
  for(int i=0; i < output.length; i++) {
    output[i] = (short)(((0xFF & input[2*i+1]) << 8) | (0xFF & input[2*i]));
    output[i] /= Short.MAX_VALUE; // normalise to [-1,1]
  }
   
}
 
    public void decode(double[] buffer, double[] fftbuf) {
         double dataMax = Short.MAX_VALUE;
    double dataMin = Short.MIN_VALUE;
    double range = dataMax - dataMin;

    }
    
}    
    
    
    
    



